/* 
* @Author: obsidian
* @Date:   2015-12-15 19:59:08
* @Last Modified by:   obsidian
* @Last Modified time: 2016-04-02 08:56:17
*/

#include "stopsteer.h"
#include "driver/sd/file.h"
#include "control/car.h"
#include "module/GPS/GPS.h"
#include "module/brake/brake.h"
#include "driver/delay/inc/delay.h"
#include "module/gpsteer/gpsteer.h"


extern struct GPSpath gpspath;
extern int timecd;

static void stopsteer_control();

struct Stopsteer stopsteer =
{
    .CCRx          =   (unsigned short *)&TIM2->CCR3,         //CCRx
    .init          =   nowtime_init,                     //
	.control	   =   stopsteer_control,
};

static void stopsteer_control()
{
    if(gps.speed < 10 && gps.front_point.y > 12150 && stopsteer.stop_flag == 0)//杆撞到的时候  小车的y在12302-12339左右 考虑空转的情况 这个值设置得比较大比较保险
    {
		//刹车
		brake.set_ccr(brake.stop_value);
		delay_ms(100);
		if(gps.direction == 0)//右边
		{
	//		stopsteer_init(stopsteer.midvalue0);
			*stopsteer.CCRx = stopsteer.limit[0];//收回撞杆机构
			gpsteer.release();
		}
		else
		{
	//		stopsteer_init(stopsteer.midvalue1);
			*stopsteer.CCRx = stopsteer.limit[1];//收回撞杆机构	
			gpsteer.release();
		}
			stopsteer.stop_flag = 1;
    }
}


// unsigned char stop_flag = 0;
// static void stopsteer_control()
// {
//      TIM_OCInitTypeDef  TIM_OCInitStruct;
//     
//     if(gps.front_point.y > 1100 && stop_flag == 0)
//     {
// 		 //使能时钟通道 让舵机工作
//         TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1;  //ÉèÎªPWMÊä³öÄ£Êœ
//         TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;//
//         
//         TIM_OCInitStruct.TIM_Pulse=stopsteer.midvalue;//ÉèÖÃ50%ÕŒ¿Õ±È
//         
//         TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High;//
//         TIM_OC3Init(TIM2,&TIM_OCInitStruct);//³õÊŒ»¯TIM2µÄchannel 4(pwm3)
//         TIM_OC3PreloadConfig(TIM2,TIM_OCPreload_Enable);
// 		
//         *stopsteer.CCRx = stopsteer.limit[0];//伸出撞杆机构
//         stop_flag = 1;
//     }
//     if(gps.front_point.y > 1110 && stop_flag == 1)
//     {
//         //失能时钟通道 保护舵机
//         TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Disable;//
//         TIM_OC3Init(TIM2,&TIM_OCInitStruct);
//         stop_flag = 2;
//     }
//     if(stop_flag == 2 && gps.speed < 5 && gps.front_point.y > 1239)
//     {
//         //刹车
//         //brake.set_ccr(1200);
//         		
//         TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1;  //ÉèÎªPWMÊä³öÄ£Êœ
//         TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;//
//         
//         TIM_OCInitStruct.TIM_Pulse=stopsteer.limit[0];//ÉèÖÃ50%ÕŒ¿Õ±È
//         
//         TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High;//
//         TIM_OC3Init(TIM2,&TIM_OCInitStruct);//³õÊŒ»¯TIM2µÄchannel 4(pwm3)
//         TIM_OC3PreloadConfig(TIM2,TIM_OCPreload_Enable);
//         TIM_Cmd(TIM2,ENABLE);//Ê¹ÄÜÍâÉè 
// 		
//         *stopsteer.CCRx = stopsteer.midvalue;//伸出撞杆机构
// 		
//         stop_flag = 3;
//     }
// }

void nowtime_init(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; 
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; 
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; 
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
    NVIC_Init(&NVIC_InitStructure); 
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 , ENABLE);
    TIM_TimeBaseStructure.TIM_Prescaler = 7200-1; 
    TIM_TimeBaseStructure.TIM_Period = 2500*4; // 2500 - 25ms
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; 
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
    TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
    TIM_Cmd(TIM3,ENABLE);
}


